package edu.boun.g4.coursity.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.boun.g4.coursity.domain.Comment;
import edu.boun.g4.coursity.service.CommentService;

/**
 * @author mertusta
 * @author bulutkorkmaz
 * 
 *         Handles and retrieves comment request
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

	@Resource(name = "commentService")
	private CommentService commentService;

	@RequestMapping(method = RequestMethod.POST, value = "/add")
	public @ResponseBody
	Response addComment(@CookieValue("id") int id, @RequestParam String message,
			@RequestParam Integer courseId) {

		Comment comment = new Comment();
		comment.setCommentMessage(message);
		comment.setCommentDate(new Date());

		Response response = new Response();
		response.setBody(comment);
		response.setStatus(true);

		commentService.add(courseId, id, comment);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/delete")
	public @ResponseBody
	Response deleteRating(@RequestParam Integer id) {

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		commentService.delete(id);

		return response;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/edit")
	public @ResponseBody
	Response editcomment(@RequestParam String message) {

		Comment existingComment = new Comment();
		existingComment.setCommentMessage(message);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		commentService.edit(existingComment);

		return response;
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	Response listUser() {

		List<Comment> all = commentService.getAll();

		Response response = new Response();
		response.setBody(all);
		response.setStatus(true);

		return response;
	}

	@RequestMapping(value = "/test")
	public @ResponseBody
	Response test() {

		Comment newComment1 = new Comment();
		newComment1.setCommentMessage("cok guzel");
		commentService.add(1, 1, newComment1);

		Comment newComment2 = new Comment();
		newComment2.setCommentMessage("Muthis");
		commentService.add(1, 2, newComment2);

		Comment newComment3 = new Comment();
		newComment3.setCommentMessage("Eh iste");
		commentService.add(1, 3, newComment3);

		Response response = new Response();
		response.setBody(null);
		response.setStatus(true);

		return response;
	}

}